package com.loong.mapper;

import com.loong.pojo.Address;
import org.apache.ibatis.annotations.*;

import java.util.List;

public interface AddressMapper {
    /**
     * 根据用户id查询地址列表
     * @param uid 用户id
     * @return 地址列表
     */
    @Select("SELECT * FROM address WHERE uid = #{uid} order by defaultAddress desc")
    List<Address> selectAddressByUid(@Param("uid") int uid);

    /**
     * 插入地址
     * @param address 地址对象
     */
    @Insert( "INSERT INTO address (uid, recipient, phone, detailedAddress, defaultAddress) " +
            "VALUES (#{address.uid}, #{address.recipient}, #{address.phone}, #{address.detailedAddress}, #{address.defaultAddress})")
    void insertAddress(@Param("address") Address address);

    /**
     * 根据地址id删除地址
     * @param aid 地址id
     */
    @Delete("DELETE FROM address WHERE aid = #{aid}")
    void deleteAddressByAid(int aid);

    /**
     * 更新地址为普通地址
     */
    @Update("UPDATE address SET defaultAddress = 0 WHERE defaultAddress = 1" )
    void updateAddressToCommon();

    /**
     * 更新地址为默认地址
     * @param aid 地址id
     */
    @Update("UPDATE address SET defaultAddress = 1 WHERE aid = #{aid}")
    void updateAddressToDefault(int aid);

    /**
     * 更新地址信息
     * @param address 地址对象
     */
    @Update("UPDATE address SET recipient = #{address.recipient}, phone = #{address.phone}, " +
            "detailedAddress = #{address.detailedAddress} WHERE aid = #{address.aid}" )
    void updateAddress(@Param("address") Address address);
}
